package upmc.fr.android.rss.listener;

import java.text.SimpleDateFormat;
import java.util.List;

import upmc.fr.android.model.RSSArticle;
import upmc.fr.android.rss.view.ViewRssArticle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ListenerArticles implements OnClickListener{

	private Activity parentActivity;
	private List<RSSArticle> articles;


	/**
	 * Constructeur
	 */
	public ListenerArticles (Activity parentActivity, List<RSSArticle> articles) {
		super();
		this.parentActivity = parentActivity;
		this.articles = articles;

	}

	@Override
	public void onClick(View view) {
		Button button = (Button)view;
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy-hh:mm");
		for (RSSArticle article : articles) {
			if (button.getText().equals(sdf.format(article.getDate())+" \n"+article.getTitle())) {
				Intent i = new Intent(view.getContext(), ViewRssArticle.class);
				i.putExtra("valueUrl", article.getUrl());
				parentActivity.startActivity(i);
			}
		}
	}

}
